# ******************************************************************
#
# Copyright 2018 Samsung Electronics All Rights Reserved.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# libcloud (static library) build script
##

Import('env')

libcloud_env = env.Clone()

target_os = libcloud_env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
with_upstream_libcoap = libcloud_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
    libcloud_env.AppendUnique(CPPPATH=['#extlibs/libcoap/libcoap/include'])
else:
    libcloud_env.AppendUnique(CPPPATH=['#/resource/csdk/connectivity/lib/libcoap-4.1.1/include'])

if libcloud_env.get('RELEASE') == '0':
    libcloud_env.AppendUnique(CPPPATH=['#/out/linux/x86_64/debug/resource/c_common'])
else:
    libcloud_env.AppendUnique(CPPPATH=['#/out/linux/x86_64/release/resource/c_common'])

libcloud_env.PrependUnique(CPPPATH=[
    '#/resource/csdk/security/provisioning/include',
    '#/resource/csdk/security/provisioning/include/cloud',
    '#/resource/csdk/security/provisioning/include/internal',
    '#/resource/csdk/security/include',
    '#/resource/csdk/security/include/internal',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/stack/include/internal',
    '#/resource/csdk/logger/include',
    '#/resource/csdk/connectivity/api',
    '#/resource/csdk/connectivity/common/inc',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/c_common/octhread/include',
    '#/extlibs/tinycbor/tinycbor/src',
    '#/resource/csdk/security/include/experimental'
])

if target_os not in ['windows']:
    libcloud_env.AppendUnique(CPPDEFINES=['WITH_POSIX', '_GNU_SOURCE'])
    libcloud_env.AppendUnique(CFLAGS=['-std=c99', '-Wall'])

if target_os in ['darwin', 'ios']:
    libcloud_env.AppendUnique(CPPDEFINES=['_DARWIN_C_SOURCE'])

if libcloud_env.get('LOGGING'):
    libcloud_env.AppendUnique(CPPDEFINES=['TB_LOG'])

if libcloud_env.get('MULTIPLE_OWNER') == '1':
    libcloud_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])

if target_os not in ['msys_nt', 'windows']:
    libcloud_env.PrependUnique(LIBS=['connectivity_abstraction'])

######################################################################
# Source files and Targets
######################################################################

if libcloud_env.get('WITH_TCP') == True and libcloud_env.get('WITH_CLOUD') == True:
    cloud_src = [
        'auth.c',
        'cloudresource.c',
        'utils.c',
        'crl.c',
        'aclgroup.c',
        'aclinvite.c',
        'aclid.c',
        'csr.c',
        'config.c'
    ]
    libcloud = libcloud_env.StaticLibrary('cloud', cloud_src, OBJPREFIX='libcloud_')
    libcloud_env.InstallTarget(libcloud, 'cloud')
